package com.sys.gradepaysys.controller;

import com.sys.gradepaysys.model.*;
import com.sys.gradepaysys.service.EventService;
import com.sys.gradepaysys.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("event")
/**
 * 事件处理
 * @version V1.0
 * @Package com.asiainfo.core.*.*.*.*
 * @date  
 */
public class EventController {
    @Autowired
    EventService eventService;
    @Autowired
    StudentService studentService;

    @RequestMapping("/selectEventList")
    @ResponseBody
    public ResultData selectEventList() {
        List<Event> eventListIsDoing = eventService.getEventListIsDoing();
        log.info(eventListIsDoing.toString());
        return new ResultData<>(200, null, eventListIsDoing);
    }

    @RequestMapping("/selectEventList2")
    @ResponseBody
    public ResultData selectEventList2() {
        List<Event> eventListIsDoing = eventService.getEventListNotDoing();
        log.info(eventListIsDoing.toString());

        return new ResultData<>(200, null, eventListIsDoing);
    }

    @RequestMapping("/getIsOkEvent")
    @ResponseBody
    public ResultData getIsOkEvent() {
        List<Event> eventListIsDoing = eventService.getEventListIsOk();
        log.info(eventListIsDoing.toString());
        return new ResultData<>(200, null, eventListIsDoing);
    }

    @RequestMapping("/getEventInfoStu")
    @ResponseBody
    public ResultData getEventInfoStu(String eid) {
        log.info(eid);

        List<Map<String, String>>  eventInfoStu= eventService.getEventInfoStu(eid);
        return new ResultData<>(200, null, eventInfoStu);
    }

    @RequestMapping("/supportStart")
    @ResponseBody
    public ResultData supportStart(String eid) {
        log.info(eid);
        eventService.supportStart(Integer.valueOf(eid));
        return  new ResultData<>(200,"投票开始!",null);

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultData add(String ename, String epay, String sdate, String edate, String grade, String clazz) {
        List<Clazz> clazzes = studentService.selectClazzByGid(null, grade, clazz);
        Clazz clazz1 = new Clazz();
        for (Clazz clazz2 : clazzes) {
            clazz1 = clazz2;
        }
        Event event = new Event();
        event.setCid(clazz1.getCid());
        event.setEName(ename);
        event.setEPay(Double.valueOf(epay));
        event.setSTime(sdate);
        event.setETime(edate);
        event.setENum(0);
        log.info(event.toString());

        boolean b = eventService.addEvent(event);
        ResultData resultData = null;
        if (b) {
            resultData = new ResultData<>(200, "成功添加事件!", null);
        } else {
            resultData = new ResultData<>(200, "成功添加失败!", null);
        }
        return resultData;

    }

    @RequestMapping("/getGrade")
    @ResponseBody
    public ResultData getGrade(Model model) {
        List<Grade> allGrade = studentService.getAllGrade();
        model.addAttribute("allGrade", allGrade);
        return new ResultData<>(200, "获取成功!", allGrade);
    }

    @RequestMapping("/getClazz")
    @ResponseBody
    public ResultData getClazz(String gid) {
        List<Clazz> allClazz = studentService.getAllClazz(gid);
        return new ResultData<>(200, "获取成功!", allClazz);

    }

    @RequestMapping("/getEventByEid")
    @ResponseBody
    public ResultData getEventByEid(HttpServletRequest request, String eid) {
        Event eventByEid = eventService.getEventByEid(Integer.valueOf(eid));
        request.getSession().setAttribute("ename", eventByEid.getEName());
        return new ResultData<>(200, null, eventByEid);

    }

    @RequestMapping("/supportEvent")
    @ResponseBody
    public ResultData getEventByEid(HttpServletRequest request) {
        ResultData resultData = null;
        Student stu = (Student) request.getSession().getAttribute("user");
        String support = request.getParameter("support");
        String eid = request.getParameter("eid");
        Integer sid = stu.getsId();
        boolean b1 = eventService.checkStuEventSupport(sid, Integer.valueOf(eid));
        if (b1) {
            try {
                boolean b = eventService.supportEvent(Integer.valueOf(eid), support);
                if (b) {
                    resultData = new ResultData<>(200, "支持成功!", null);
                } else {
                    resultData = new ResultData<>(200, "反对成功!", null);
                }
            } catch (Exception e) {
                new ResultData<>(500, e.getMessage(), null);

            }
        } else {
            resultData = new ResultData<>(200, "该事件已经投过票！", null);

        }
        return resultData;

    }
    
    @RequestMapping("/getEventStu")
    @ResponseBody
    public ResultData getEventStu(HttpServletRequest request) {
        String eid = request.getParameter("eid");
        List<Map<String, String>> eventStu = eventService.getEventStu(eid);
        return new ResultData<>(200, null, eventStu);

    }

    @RequestMapping("/toPay")
    @ResponseBody
    public ResultData toPay(HttpServletRequest request) {
        ResultData resultData = null;
        Student stu = (Student) request.getSession().getAttribute("user");

        String eid = request.getParameter("eid");
        String pay = request.getParameter("pay");
        boolean b = eventService.StuToPay(String.valueOf(stu.getsId()), eid, pay);
        if (b){
            resultData=new ResultData<>(200, "捐助成功!", null);
        }else {
            resultData=new ResultData<>(200, "捐助失败!", null);
        }
        return resultData;

    }
    @RequestMapping("/echarts")
    public String echarts() {
        return "echarts";
    }
    
    @RequestMapping("/toPayPage")
    public String toPay() {
        return "toPay";
    }
    @RequestMapping("/program")
    public String program() {
        return "program";
    }
}
